<?php
require 'page.class.php';
    $connetc=mysql_connect("localhost","root","192510");
	mysql_select_db("news",$connetc);
	//$sql="select * from user";
	//$result=mysql_query($sql);
	session_start();
	$status = $_SESSION['status'];


	$sql2="select count(*) as amount from user";
	$result2=mysql_query($sql2);
	$row2=mysql_fetch_array($result2);
	$count=$row2['amount'];//总条数
	$pagesize=10;//每页显示的条数
	if($count%$pagesize){
		$page=ceil($count/$pagesize);
	}else{
		$page=$count/$pagesize;
	}
	if(isset($_GET['currentpage'])){
		$currentpage=intval($_GET['currentpage']);
    }else{
		$currentpage=1;
    }
	$start=($currentpage-1)*$pagesize;
    $sql="select * from user limit $start,$pagesize";
    $result = mysql_query($sql);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-1.6.3.min.js"></script>
<style>
      .tip{
		  width:600px;
		  height:280px;
	  }

</style>
<script type="text/javascript">
$(document).ready(function(){
  $(".click").click(function(){
     $(".tip").fadeIn(200);
  });
  
  $(".tiptop a").click(function(){
     $(".tip").fadeOut(200);
  });
  
  
});
</script>

</head>

<body>

	<div class="place">
    <span>位置：</span>
    <ul class="placeul">
    <li><a href="#">用户管理</a></li>
    <li><a href="#">用户列表</a></li>
    </ul>
    </div>
	
    <div class="rightinfo">
    
    <div class="tools">
    	<ul class="toolbar">
        <li class="click" onclick="ck(0)"><span><img src="images/t01.png" /></span>添加</li>
        <li onclick="deletebatch()"><span><img src="images/t03.png"/></span>删除</li>
        </ul>
    </div>
	
    <table class="tablelist">
    	<thead>
    	<tr>
        <th><input  type="checkbox" name="checkbox" onclick="selectAll(this)"/></th>
        <th>用户名</th>
        <th>权限</th>
        <th>基本操作</th>
        </tr>
        </thead>
        <tbody>
		<?php
		while($row=mysql_fetch_array($result)){
		?>
		<tr>
        <td><input  type="checkbox" name="son_checkbox" value="<?php echo $row['id'];?>"/></td>
        <td><?php echo $row['username'];?></td>
        <td><?php if($row['status']==0){echo "管理员";}else if($row['status']==1){echo "普通用户";}else if($row['status']==2){echo "超级管理员";}?></td>
        <td>
        <a  onclick="ck('<?php echo $row['id'];?>');" class="tablelink" href="#">修改</a> 
        <a href="delete_user.php?id=<?php echo $row['id'];?>" class="tablelink"> 删除</a>
        </td>
        </tr> 
		<?php
	     }	
		?>
        </tbody>
    </table>
	
    <div class="pagin">
    	<?php 
    		pagelinks();
    	?>
    </div>
	
    <div class="tip">
	  <div class="tiptop"><span>添加用户</span><a></a></div>
        
      <div class="formbody">
      <form method="post" action="insert_user_commit.php">
      <ul class="forminfo">
	  <input size="20" type="hidden" name="id" id="id">
      <li><label>用户名</label><input name="username" id="username" type="text" onblur="user();" class="dfinput" value="<?php echo $row['username'];?>" /><i style="display:none;" id="yz_user">用户名不能为空</i></li>
      <li><label>密码</label><input name="password" id="password" type="password" class="dfinput" onblur="pass();" value="<?php echo $row['password'];?>"/><i style="display:none;" id="yz_pass">密码不能为空</i></li>
      <li><label>用户状态</label><select  name="status" id="status">
						<?php 
      						if($status==2){
      					?>
								<option value="1"  <?php echo (isset($row)&&$row['status']==0)?'selected=selected':'';?>>普通用户</option>
								<option value="0"  <?php echo (isset($row)&&$row['status']==1)?'selected=selected':'';?>>管理员</option>
								<option value="2"  <?php echo (isset($row)&&$row['status']==2)?'selected=selected':'';?>>超级管理员</option>
								
						<?php 
							}else{
						?>
							<option value="1"  <?php echo (isset($row)&&$row['status']==0)?'selected=selected':'';?>>普通用户</option>
							<option value="0"  <?php echo (isset($row)&&$row['status']==1)?'selected=selected':'';?>>管理员</option>

						<?php 
							}
						?>
						</select>
		</li>
      <li><label>&nbsp;</label><input name="" type="submit" class="btn" value="确认保存" onclick="return tijiao();"/></li>
      </ul>
	  </form>
	  
      </div>
	  
      <!--
	  <div class="tipbtn">
      <input name="" type="button"  class="sure" value="确定" />&nbsp;
      <input name="" type="button"  class="cancel" value="取消" />
      </div>
	  -->
	  
    </div>
	
    </div>
    
    <script type="text/javascript">
	$('.tablelist tbody tr:odd').addClass('odd');
	
	function ck(id){
		if(id==0){//添加  
		
		   $("#id").val("");
		   $("#username").val("");
		   $("#password").val("");
		   $("#status").val(0);
			
		}else{  //编辑
			$.post("select_user_id.php",{id:id},function(data){
				   $("#id").val(data['id']);
				   $("#username").val(data['username']);
				   $("#password").val(data['password']);
				   $("#status").val(data['status']);
				   $(".tip").fadeIn(200);
			},"json")
		}
		
	}
	function selectAll(objs){
	    obj=document.getElementsByName('son_checkbox');
		for(var i=0;i<obj.length;i++){
			if(objs.checked){
				obj[i].checked=true;
			}else{
				obj[i].checked=false;
			}
			
		}
	}
	
	function deletebatch(){
	      obj=document.getElementsByName('son_checkbox');
		  var arrayid=new Array();
		  for(var i=0;i<obj.length;i++){
			  if(obj[i].checked){
				 var value=obj[i].value;
				 arrayid.push(value);
			  }
			  
		  }
		  
		  if(arrayid.length==0){
			  alert("请选择需要删除的任务");
		  }else{
			  
			  if(confirm("确定要删除所选择的新闻吗")){
				  
				  $.post('delete_user.php',{arrayid:arrayid},function(data){
				  window.location.reload();
			    });
				  
			  }else{
				  return false;
			  }
			  
		  }
		  
	}
	
	
   function tijiao(){
		  var username=$("#username").val();
		  var password=$("#password").val();
		  
		  if(username==null||username.length==0||password==null||password.length==0){
			  alert("用户名或密码不能为空");
			  return false;
			 
		  }else{
			   return true;
		  }
		 
	  }
	  
	  
	   function user(){
		 //根据特殊条件做出特殊的处理
		var username=$("#username").val();
		if(username==null||username.length==0){
			$("#yz_user").show();
			
		}else{
			$("#yz_user").hide();
		}
	 }
	 
	  
	 function pass(){
		 var password=$("#password").val();
		 if(password==null||password.length==0){
			 $("#yz_pass").show();
		 }else{
			 $("#yz_pass").hide();
		 }
	 }
	  
	</script>

</body>

</html>
